﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Sausage.Common;
using Sausage.Web.Code;
using Sausage.Web.DataModel;
using Sausage.Web.Domain;
using Sausage.Web.Models;

namespace Sausage.Web.Controllers
{
    /// <summary>
    /// 项目人工控制器
    /// </summary>
    /// <remarks></remarks>
    public class ProjectLabourController : Controller
    {
        /// <summary>
        /// 默认动作。
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        [RequireAuthorization]
        [HandleError(Order = 3, ExceptionType = typeof(UnauthenticatedException), View = "Unauthenticated")]
        [HandleError(Order = 2, ExceptionType = typeof(UnauthorizedException), View = "Unauthorized")]
        [HandleError(Order = 1, ExceptionType = typeof(Exception), View = "Exception")]
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 审核。
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        [RequireAuthorization]
        [HandleError(Order = 3, ExceptionType = typeof(UnauthenticatedException), View = "Unauthenticated")]
        [HandleError(Order = 2, ExceptionType = typeof(UnauthorizedException), View = "Unauthorized")]
        [HandleError(Order = 1, ExceptionType = typeof(Exception), View = "Exception")]
        public ActionResult Audit()
        {
            return View();
        }

        /// <summary>
        /// 默认动作。
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        [RequireAuthorization]
        [HandleError(Order = 3, ExceptionType = typeof(UnauthenticatedException), View = "Unauthenticated")]
        [HandleError(Order = 2, ExceptionType = typeof(UnauthorizedException), View = "Unauthorized")]
        [HandleError(Order = 1, ExceptionType = typeof(Exception), View = "Exception")]
        public ActionResult Submit()
        {
            return View();
        }

        /// <summary>
        /// 确认。
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        [RequireAuthorization]
        [HandleError(Order = 3, ExceptionType = typeof(UnauthenticatedException), View = "Unauthenticated")]
        [HandleError(Order = 2, ExceptionType = typeof(UnauthorizedException), View = "Unauthorized")]
        [HandleError(Order = 1, ExceptionType = typeof(Exception), View = "Exception")]
        public ActionResult Confirm()
        {
            return View();
        }

        /// <summary>
        /// 签审。
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        [RequireAuthorization]
        [HandleError(Order = 3, ExceptionType = typeof(UnauthenticatedException), View = "Unauthenticated")]
        [HandleError(Order = 2, ExceptionType = typeof(UnauthorizedException), View = "Unauthorized")]
        [HandleError(Order = 1, ExceptionType = typeof(Exception), View = "Exception")]
        public ActionResult Sign()
        {
            return View();
        }

        /// <summary>
        /// 查看工程材料。
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        [HandleError(Order = 3, ExceptionType = typeof(UnauthenticatedException), View = "Unauthenticated")]
        [HandleError(Order = 2, ExceptionType = typeof(UnauthorizedException), View = "Unauthorized")]
        [HandleError(Order = 1, ExceptionType = typeof(Exception), View = "Exception")]
        public ActionResult ViewProjectLabour(Guid projectID)
        {
            //返回视图
            return View(ProjectHelper.GetProjectByID(projectID));
        }

        /// <summary>
        /// 查看工程材料。
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        [HandleError(Order = 3, ExceptionType = typeof(UnauthenticatedException), View = "Unauthenticated")]
        [HandleError(Order = 2, ExceptionType = typeof(UnauthorizedException), View = "Unauthorized")]
        [HandleError(Order = 1, ExceptionType = typeof(Exception), View = "Exception")]
        public ActionResult ViewQuantityLabour(Guid quantityID)
        {
            //返回视图
            return View(ProjectQuantityHelper.GetProjectQuantityByID(quantityID));
        }

        /// <summary>
        /// 查询工程人工数据
        /// </summary>
        /// <param name="query">查询条件模型。</param>
        /// <returns>查询的工程人工信息列表</returns>
        /// <remarks></remarks>
        [HttpGet]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult SubmitSelect(JosnPagingQueryModel query)
        {
            var result = ProjectLabourHelper.SubmitSelect(query);
            return new JsonNetResult()
            {
                Data = new JsonResultViewModel
                {
                    success = true,
                    data = result.Data,
                    total = result.Total,
                    message = "项目工程人工加载成功！"
                },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }

        /// <summary>
        /// 查询工程人工数据
        /// </summary>
        /// <param name="query">查询条件模型。</param>
        /// <returns>查询的工程人工信息列表</returns>
        /// <remarks></remarks>
        [HttpGet]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult ConfirmSelect(JosnPagingQueryModel query)
        {
            var result = ProjectLabourHelper.ConfirmSelect(query);
            return new JsonNetResult()
            {
                Data = new JsonResultViewModel
                {
                    success = true,
                    data = result.Data,
                    total = result.Total,
                    message = "项目工程人工加载成功！"
                },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }

        /// <summary>
        /// 查询工程人工数据
        /// </summary>
        /// <param name="query">查询条件模型。</param>
        /// <returns>查询的工程人工信息列表</returns>
        /// <remarks></remarks>
        [HttpGet]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult SignSelect(JosnPagingQueryModel query)
        {
            var result = ProjectLabourHelper.SignSelect(query);
            return new JsonNetResult()
            {
                Data = new JsonResultViewModel
                {
                    success = true,
                    data = result.Data,
                    total = result.Total,
                    message = "项目工程人工加载成功！"
                },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }

        /// <summary>
        /// 查询工程人工数据。
        /// </summary>
        /// <param name="query">查询条件模型。</param>
        /// <returns>查询的工程人工信息列表</returns>
        /// <remarks></remarks>
        [HttpGet]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult AuditSelect(JosnPagingQueryModel query)
        {
            var result = ProjectLabourHelper.AuditSelect(query);
            return new JsonNetResult()
            {
                Data = new JsonResultViewModel
                {
                    success = true,
                    data = result.Data,
                    total = result.Total,
                    message = "项目工程人工加载成功！"
                },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }

        [HttpGet]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult Select(JosnPagingQueryModel query)
        {
            var result = ProjectLabourHelper.Select(query);
            return new JsonNetResult()
            {
                Data = new JsonResultViewModel
                {
                    success = true,
                    data = result.Data,
                    total = result.Total,
                    message = "项目工程人工加载成功！"
                },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }

        /// <summary>
        /// 详细
        /// </summary>
        /// <returns>执行结果</returns>
        /// <remarks></remarks>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult Details(Guid labourID)
        {
            var item = ProjectLabourHelper.GetProjectLabourByID(labourID);

            if (item == null)
                return new JsonNetResult()
                {
                    Data = new JsonResultViewModel
                    {
                        success = false,
                        message = "指定的项目工程人工不存在！"
                    },
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet
                };

            var data = new
            {
                item.LabourID,
                item.QuantityID,
                item.OrganizationID,
                Organization = item.Organization.OrganizationName,
                item.ProjectID,
                ProjectCode = item.Project.ProjectCode,
                ProjectName = item.Project.ProjectName,
                ConstructionID = item.ProjectQuantity.ConstructionID,
                Construction = item.ProjectQuantity.Construction.ConstructionName,
                item.JobID,
                Job = item.Job.ItemName,
                item.LabourTypeID,
                LabourType = item.LabourType.ItemName,
                item.Quantity,
                item.Price,
                item.Amount,
                item.CreateDate,
                item.Applicant,
                item.ApplicationDate,
                item.Auditor,
                item.AuditedDate,
                item.Approver,
                item.ApprovedDate,
                item.Signer,
                item.SignedDate,
                item.State,
                item.IsDelay,
                item.Memo
            };

            return new JsonNetResult()
            {
                Data = new JsonResultViewModel
                {
                    success = true,
                    data = data,
                    total = 1,
                    message = "项目工程人工加载成功！"
                },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <returns>执行结果</returns>
        /// <remarks></remarks>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult Delete(ProjectLabourListRow data)
        {
            ProjectLabourHelper.DeleteProjectLabour(data.LabourID);
            return Json(new JsonResultViewModel { success = true, message = "工程人工删除成功！" }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <returns>执行结果</returns>
        /// <remarks></remarks>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult Update(ProjectLabourListRow data)
        {
            ProjectLabourHelper.UpdateProjectLabour(data);
            return Json(new JsonResultViewModel { success = true, message = "工程人工更新成功！" }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 更新状态
        /// </summary>
        /// <returns>执行结果</returns>
        /// <remarks></remarks>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult UpdateState(Guid id, int state)
        {
            ProjectLabourHelper.UpdateProjectLabourState(id, state);
            return Json(new JsonResultViewModel { success = true, message = "工程人工状态更新成功！" }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 创建
        /// </summary>
        /// <returns>创建结果。</returns>
        /// <remarks></remarks>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult Create(ProjectLabourListRow data)
        {
            string errorMessage;
            if (!Helper.CouldSet(data.ApplicationDate, out errorMessage))
                return Json(new JsonResultViewModel { success = false, message = errorMessage }, JsonRequestBehavior.AllowGet);

            ProjectLabourHelper.CreateProjectLabour(data);
            return Json(new JsonResultViewModel { success = true, message = "工程人工创建成功！" }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 提交
        /// </summary>
        /// <param name="projectID">The project ID.</param>
        /// <returns>
        /// 执行结果
        /// </returns>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult ExecuteSubmit(Guid[] labours)
        {
            ProjectLabourHelper.ExecuteSubmit(labours);
            return Json(new JsonResultViewModel { success = true, message = "项目工程人工提交成功！" }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 审核
        /// </summary>
        /// <param name="labours">The labours.</param>
        /// <param name="name">The auditor.</param>
        /// <returns>
        /// 执行结果
        /// </returns>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult ExecuteAudit(Guid[] labours, bool passed, string comment)
        {
            ProjectLabourHelper.ExecuteAudit(labours, passed, comment);
            return Json(new JsonResultViewModel { success = true, message = "项目工程人工审核成功！" }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 批准
        /// </summary>
        /// <param name="labours">The labours.</param>
        /// <param name="auditor">The auditor.</param>
        /// <returns>
        /// 执行结果
        /// </returns>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult ExecuteConfirm(Guid[] labours, bool passed, string comment)
        {
            ProjectLabourHelper.ExecuteConfirm(labours, passed, comment);
            return Json(new JsonResultViewModel { success = true, message = "项目工程人工审核成功！" }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 签审
        /// </summary>
        /// <param name="labours">The labours.</param>
        /// <param name="auditor">The auditor.</param>
        /// <returns>
        /// 执行结果
        /// </returns>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult ExecuteSign(Guid[] labours, bool passed)
        {
            ProjectLabourHelper.ExecuteSign(labours, passed);
            return Json(new JsonResultViewModel { success = true, message = "工程人工签审成功！" }, JsonRequestBehavior.AllowGet);
        }
    }
}
